<?php

namespace app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\service\v1\CommissionService;
use plugin\admin\app\service\v1\OptionService;
use plugin\admin\app\service\v1\OrderNodeService;
use support\Log;
use support\Request;
use support\exception\BusinessException;

/**
 * 定时任务
 **/
class CrontabController
{

    /**
     * 订单到期-1分钟/次
     **/
    public function orderExpire(Request $request)
    {
        if ($request->method() === 'POST') {
            try {
                OrderNodeService::orderExpire();
            } catch (BusinessException $e) {
                return json(['code' => 1, 'msg' => "Code: " . $e->getCode() . " Line: " . $e->getLine() . " Message: " . $e->getMessage() . " File: " . $e->getFile()]);
            }
            return json(['code' => 0, 'msg' => 'success']);
        }
    }

    /**
     * 订单节点保留到期-3分钟/次
     **/
    public function orderReserveExpire(Request $request)
    {
        if ($request->method() === 'POST') {
            try {
                OrderNodeService::orderReserveExpire();
            } catch (BusinessException $e) {
                return json(['code' => 1, 'msg' => "Code: " . $e->getCode() . " Line: " . $e->getLine() . " Message: " . $e->getMessage() . " File: " . $e->getFile()]);
            }
            return json(['code' => 0, 'msg' => 'success']);
        }
    }

    /**
     * 获取自动续费列表-12小时/次
     **/
    public function renewAuto(Request $request){
        if ($request->method() === 'POST') {
            try {
                OrderNodeService::renewAuto();
            } catch (BusinessException $e) {
                return json(['code' => 1, 'msg' => "Code: " . $e->getCode() . " Line: " . $e->getLine() . " Message: " . $e->getMessage() . " File: " . $e->getFile()]);
            }
            return json(['code' => 0, 'msg' => 'success']);
        }
    }

    public function ceshi(Request $request)
    {
        $db = Util::db();
        $aa = $db->table('wa_v1_order_node')->where('deleted',1)->pluck('id')->toArray();
        dump($aa);
        return json(['code' => 0, 'msg' => 'success', 'data' => []]);
//        $data = ['commission_type' => CommonConstant::DEFAULT_1, 'type' => CommonConstant::MONEY_LOG_TYPE_5, 'money_log_id' => 5];
//        CommissionService::index($data);
//        $data = OrderNodeService::orderReserveExpire();
//        return json(['code' => 0, 'msg' => 'success', 'data' => $data]);

//        // 删除
//        $orderNodeList = Util::db()->table('wa_v1_order_node')->get();
//        $data = [];
//        foreach ($orderNodeList as $orderNode) {
//            $array = explode('|', $orderNode->data);
//            $url = $array[0];
//            $apiData = [];
//            $apiData['key'] = $array[1];
//            $apiData['username'] = $orderNode->account;
//            $result = Util::userApi('del', $url, $apiData);
//            $data[] = $result;
//        }

//        $function = $request->input('function');
//        $url = 'http://182.43.75.195:11352/user/';
//        $api_data['key'] = 'dxystd8750';
//        $api_data['username'] = '4zabdi3z0';
//        $res = Util::userApi($function, $url, $api_data);
//        return json(['code' => 0, 'msg' => 'success','data'=>$res]);
//
//        $data = ['commission_type' => CommonConstant::DEFAULT_1, 'type' => CommonConstant::MONEY_LOG_TYPE_4, 'money_log_id' => 13];
//        $log = Log::channel('commission_queue');
//        $log->info('队列：返佣开始', $data);
//        try {
//            $res = CommissionService::index($data);
//        } catch (BusinessException $e) {
//            $log->info('队列：返佣结束 error'.$e->getMessage(), $data);
//            return json(['code' => 1, 'msg' => $e->getMessage()]);
//        }
//
//        // 更新返佣状态
//        Util::db()->table('wa_v1_user_money_log')
//            ->where('id', $data['money_log_id'])
//            ->update(['status' => CommonConstant::DEFAULT_1]);
//        $log->info('队列：返佣结束 success', $data);
//        return json(['code' => 0, 'msg' => 'success', 'data' => $data]);
    }

}
